package com.presalept.manipulatedata;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.presalept.polodo.EventJsonDO;
import com.presalept.polodo.JsonDataProtocolDO;
import com.presalept.polodo.ProjectJsonDO;
import com.presalept.polodo.ProjectMemberDO;
import com.presalept.polodo.ProjectMemberJsonDO;

import net.sf.json.*;

public class OperateJsonData {

	public static String GetJsonData(JsonDataProtocolDO jsondata){
		
		String strJsonData = null;
		
		JSONObject jsonObject = JSONObject.fromObject(jsondata);
		strJsonData = jsonObject.toString();

		return strJsonData;

	}
	
	//get members of the project
	
	public static ProjectMemberJsonDO GetProjectMemberListJson(String strJsonData, Class<ProjectMemberJsonDO> clazz){
		
		JSONObject jsonObject = null; 
		ProjectMemberJsonDO pmjd = null;
		//List<ProjectMemberDO> pmdo = null;
		
		Map<String, Class<ProjectMemberDO>> classmap = new HashMap<String, Class<ProjectMemberDO>>();
		
		classmap.put("jsonDataObj", ProjectMemberDO.class);
		
		try{ 

			jsonObject = JSONObject.fromObject(strJsonData); 
			pmjd = (ProjectMemberJsonDO)JSONObject.toBean(jsonObject, clazz, classmap);
			//pmdo = pmjd.getJsonDataObj();
			
		}catch(Exception e){ 
			e.printStackTrace(); 
		} 
		
		return pmjd; 	
	}
	
	//get the project data from iosapp
	
	public static ProjectJsonDO GetProjectJson(String strJsonData){
		
		JSONObject jsonObject = null;
		ProjectJsonDO pjd = null;
		
		//Map<String, Class<ProjectJsonDO>> classmap = new HashMap<String, Class<ProjectJsonDO>>();
		
		//classmap.put("jsonDataObj", ProjectMemberDO.class);
		
		try{ 

			jsonObject = JSONObject.fromObject(strJsonData); 
			pjd = (ProjectJsonDO)JSONObject.toBean(jsonObject);
			
		}catch(Exception e){ 
			e.printStackTrace(); 
		} 
		
		return pjd; 
	}
	
	//get the event data from iosapp
	
	public static EventJsonDO GetEventJson(String strJsonData){
		
		JSONObject jsonObject = null;
		EventJsonDO ejd = null;
		
		try{ 

			jsonObject = JSONObject.fromObject(strJsonData); 
			ejd = (EventJsonDO)JSONObject.toBean(jsonObject);
			
		}catch(Exception e){ 
			e.printStackTrace(); 
		} 
		
		return ejd; 
	}
	
	//the method creating input data is used to test Json
	
	public static String CreateProjectMemberList(Integer CreateCount){
		
		List<ProjectMemberDO> pmld = new ArrayList<ProjectMemberDO>();
		
		for(int i = 0; i < CreateCount; i++){
			ProjectMemberDO pmdo = new ProjectMemberDO();
			pmdo.setCreatedate("2014-04-12");
			pmdo.setOperateType(1);
			pmdo.setProjectId(i);
			pmdo.setPurelationstatus(1);
			pmdo.setUserId(i);
			pmdo.setUserName("User" + i);		
			pmld.add(pmdo);
		}
		
		JsonDataProtocolDO jdpldo = new JsonDataProtocolDO();
		
		jdpldo.setErrorCode(0);
		jdpldo.setPackageCode(1);
		jdpldo.setJsonDataObj(pmld);	
		return OperateJsonData.GetJsonData(jdpldo);	
	}
}
